EMMA Coverage Report (generated Thu Sep 08 15:52:12 CEST 2016)
[all classes][caldwell.ben.trolly]

COVERAGE SUMMARY FOR SOURCE FILE [TrollyProvider.java]

nameclass, %method, %block, %line, %
TrollyProvider.java100% (2/2)82%  (9/11)69%  (322/464)81%  (74,4/92)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class TrollyProvider$DatabaseHelper100% (1/1)67%  (2/3)31%  (11/35)50%  (4/8)
onUpgrade (SQLiteDatabase, int, int): void 0%   (0/1)0%   (0/24)0%   (0/4)
TrollyProvider$DatabaseHelper (Context): void 100% (1/1)100% (7/7)100% (2/2)
onCreate (SQLiteDatabase): void 100% (1/1)100% (4/4)100% (2/2)
     
class TrollyProvider100% (1/1)88%  (7/8)72%  (311/429)84%  (70,4/84)
getType (Uri): String 0%   (0/1)0%   (0/20)0%   (0/4)
insert (Uri, ContentValues): Uri 100% (1/1)66%  (71/107)77%  (17/22)
delete (Uri, String, String []): int 100% (1/1)68%  (48/71)88%  (9,7/11)
update (Uri, ContentValues, String, String []): int 100% (1/1)72%  (69/96)86%  (14,7/17)
query (Uri, String [], String, String [], String): Cursor 100% (1/1)85%  (66/78)94%  (16/17)
<static initializer> 100% (1/1)100% (45/45)100% (10/10)
TrollyProvider (): void 100% (1/1)100% (3/3)100% (1/1)
onCreate (): boolean 100% (1/1)100% (9/9)100% (2/2)

1/**
2        <Trolly is a simple shopping list application for android phones.>
3        Copyright (C) 2009  Ben Caldwell
4         
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19package caldwell.ben.trolly;
20 
21import caldwell.ben.provider.Trolly;
22import caldwell.ben.provider.Trolly.ShoppingList;
23 
24import android.content.ContentProvider;
25import android.content.ContentUris;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.UriMatcher;
29import android.content.res.Resources;
30import android.database.Cursor;
31import android.database.SQLException;
32import android.database.sqlite.SQLiteDatabase;
33import android.database.sqlite.SQLiteOpenHelper;
34import android.database.sqlite.SQLiteQueryBuilder;
35import android.net.Uri;
36import android.text.TextUtils;
37import android.util.Log;
38 
39import java.util.HashMap;
40 
41/**
42 * Provides access to a database of notes. Each note has a title, the note
43 * itself, a creation date and a modified data.
44 */
45public class TrollyProvider extends ContentProvider {
46 
47    private static final String TAG = "TrollyProvider";
48 
49    private static final String DATABASE_NAME = "trolly.db";
50    private static final int DATABASE_VERSION = 2;
51    private static final String TABLE_NAME = "shopping_list";
52 
53    private static HashMap<String, String> sProjectionMap;
54 
55    private static final int ITEMS = 1;
56    private static final int ITEM_ID = 2;
57 
58    private static final UriMatcher sUriMatcher;
59 
60    /**
61     * This class helps open, create, and upgrade the database file.
62     */
63    private static class DatabaseHelper extends SQLiteOpenHelper {
64 
65        DatabaseHelper(Context context) {
66            super(context, DATABASE_NAME, null, DATABASE_VERSION);
67        }
68 
69        @Override
70        public void onCreate(SQLiteDatabase db) {
71            db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
72                    + ShoppingList._ID + " INTEGER PRIMARY KEY,"
73                    + ShoppingList.ITEM + " TEXT,"
74                    + ShoppingList.STATUS + " INTEGER,"
75                    + ShoppingList.CREATED_DATE + " INTEGER,"
76                    + ShoppingList.MODIFIED_DATE + " INTEGER"
77                    + ");");
78        }
79 
80        @Override
81        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
82            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
83                    + newVersion + ", which will destroy all old data");
84            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
85            onCreate(db);
86        }
87    }
88 
89    private DatabaseHelper mOpenHelper;
90 
91    @Override
92    public boolean onCreate() {
93        mOpenHelper = new DatabaseHelper(getContext());
94        return true;
95    }
96 
97    @Override
98    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
99            String sortOrder) {
100        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
101 
102        switch (sUriMatcher.match(uri)) {
103        case ITEMS:
104            qb.setTables(TABLE_NAME);
105            qb.setProjectionMap(sProjectionMap);
106            break;
107 
108        case ITEM_ID:
109            qb.setTables(TABLE_NAME);
110            qb.setProjectionMap(sProjectionMap);
111            qb.appendWhere(ShoppingList._ID + "=" + uri.getPathSegments().get(1));
112            break;
113 
114        default:
115            throw new IllegalArgumentException("Unknown URI " + uri);
116        }
117 
118        // If no sort order is specified use the default
119        String orderBy;
120        if (TextUtils.isEmpty(sortOrder)) {
121            orderBy = ShoppingList.DEFAULT_SORT_ORDER;
122        } else {
123            orderBy = sortOrder;
124        }
125 
126        // Get the database and run the query
127        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
128        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);
129 
130        // Tell the cursor what uri to watch, so it knows when its source data changes
131        c.setNotificationUri(getContext().getContentResolver(), uri);
132        return c;
133    }
134 
135    @Override
136    public String getType(Uri uri) {
137        switch (sUriMatcher.match(uri)) {
138        case ITEMS:
139            return ShoppingList.CONTENT_TYPE;
140 
141        case ITEM_ID:
142            return ShoppingList.CONTENT_ITEM_TYPE;
143 
144        default:
145            throw new IllegalArgumentException("Unknown URI " + uri);
146        }
147    }
148 
149    @Override
150    public Uri insert(Uri uri, ContentValues initialValues) {
151        // Validate the requested uri
152        if (sUriMatcher.match(uri) != ITEMS) {
153            throw new IllegalArgumentException("Unknown URI " + uri);
154        }
155 
156        ContentValues values;
157        if (initialValues != null) {
158            values = new ContentValues(initialValues);
159        } else {
160            values = new ContentValues();
161        }
162 
163        Long now = Long.valueOf(System.currentTimeMillis());
164 
165        // Make sure that the fields are all set
166        if (values.containsKey(ShoppingList.CREATED_DATE) == false) {
167            values.put(ShoppingList.CREATED_DATE, now);
168        }
169 
170        if (values.containsKey(ShoppingList.MODIFIED_DATE) == false) {
171            values.put(ShoppingList.MODIFIED_DATE, now);
172        }
173 
174        if (values.containsKey(ShoppingList.ITEM) == false) {
175            Resources r = Resources.getSystem();
176            values.put(ShoppingList.ITEM, r.getString(android.R.string.untitled));
177        }
178 
179        if (values.containsKey(ShoppingList.STATUS) == false) {
180            values.put(ShoppingList.STATUS, Trolly.ShoppingList.ON_LIST);
181        }
182 
183        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
184        long rowId = db.insert(TABLE_NAME, ShoppingList.ITEM, values);
185        if (rowId > 0) {
186            Uri itemUri = ContentUris.withAppendedId(ShoppingList.CONTENT_URI, rowId);
187            getContext().getContentResolver().notifyChange(itemUri, null);
188            return itemUri;
189        }
190 
191        throw new SQLException("Failed to insert row into " + uri);
192    }
193 
194    @Override
195    public int delete(Uri uri, String where, String[] whereArgs) {
196        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
197        int count;
198        switch (sUriMatcher.match(uri)) {
199        case ITEMS:
200            count = db.delete(TABLE_NAME, where, whereArgs);
201            break;
202 
203        case ITEM_ID:
204            String noteId = uri.getPathSegments().get(1);
205            count = db.delete(TABLE_NAME, ShoppingList._ID + "=" + noteId
206                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
207            break;
208 
209        default:
210            throw new IllegalArgumentException("Unknown URI " + uri);
211        }
212 
213        getContext().getContentResolver().notifyChange(uri, null);
214        return count;
215    }
216 
217    @Override
218    public int update(Uri uri, ContentValues initialValues, String where, String[] whereArgs) {
219        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
220        int count;
221        
222        ContentValues values;
223        if (initialValues != null) {
224            values = new ContentValues(initialValues);
225        } else {
226            values = new ContentValues();
227        }
228 
229        Long now = Long.valueOf(System.currentTimeMillis());
230 
231        // Update the modified field
232        if (values.containsKey(ShoppingList.MODIFIED_DATE) == false) {
233            values.put(ShoppingList.MODIFIED_DATE, now);
234        }
235        
236        switch (sUriMatcher.match(uri)) {
237        case ITEMS:
238            count = db.update(TABLE_NAME, values, where, whereArgs);
239            break;
240 
241        case ITEM_ID:
242            String noteId = uri.getPathSegments().get(1);
243            count = db.update(TABLE_NAME, values, ShoppingList._ID + "=" + noteId
244                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
245            break;
246 
247        default:
248            throw new IllegalArgumentException("Unknown URI " + uri);
249        }
250 
251        getContext().getContentResolver().notifyChange(uri, null);
252        return count;
253    }
254 
255    static {
256        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
257        sUriMatcher.addURI(Trolly.AUTHORITY, "shoppinglist", ITEMS);
258        sUriMatcher.addURI(Trolly.AUTHORITY, "shoppinglist/#", ITEM_ID);
259 
260        sProjectionMap = new HashMap<String, String>();
261        sProjectionMap.put(ShoppingList._ID, ShoppingList._ID);
262        sProjectionMap.put(ShoppingList.ITEM, ShoppingList.ITEM);
263        sProjectionMap.put(ShoppingList.STATUS, ShoppingList.STATUS);
264        sProjectionMap.put(ShoppingList.CREATED_DATE, ShoppingList.CREATED_DATE);
265        sProjectionMap.put(ShoppingList.MODIFIED_DATE, ShoppingList.MODIFIED_DATE);
266    }
267}

[all classes][caldwell.ben.trolly]
EMMA 2.0.5312 (C) Vladimir Roubtsov